package com.chinasofti.SSM.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.chinasofti.SSM.pojo.User;
import com.chinasofti.SSM.service.user.UserService;
import com.chinasofti.SSM.tools.Constants;


@Controller
public class LoginController {
	private Logger log = Logger.getLogger(LoginController.class);
	@Resource
	private UserService userService;
	
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	@RequestMapping("/login.html")
	public String login(){
		log.info("login~~~~");
		return "login";
	}
	
	@RequestMapping("/doLogin")
	public String doLogin(String userCode,String userPassword,
			HttpSession session,
			Model model) {
		System.out.println("login ============ " );
		//调用service方法，进行用户匹配
		User user = userService.login(userCode,userPassword);
		if(null != user){//登录成功
				if(user.getUserPassword().equals(userPassword)){
					//放入session
					session.setAttribute(Constants.USER_SESSION, user);
					//页面跳转（frame.jsp）
					//response.sendRedirect("jsp/frame.jsp");
					return "redirect:/sys/main.html";
				}else{
					throw new RuntimeException("密码不正确");
					//model.addAttribute("error", "密码不正确");
					//return "login";
				}			
		}else{
			//页面跳转（login.jsp）带出提示信息--转发
			//request.setAttribute("error", "用户名或密码不正确");
			throw new RuntimeException("用户名不存在");
			//model.addAttribute("error", "用户名不存在");
			//request.getRequestDispatcher("login.jsp").forward(request, response);
			//return "login";
		}
	}
	
	/*@RequestMapping("/exLogin")
	public String doLogin2(String userCode,String userPassword,
			HttpSession session,
			Model model) {
		System.out.println("login ============ " );
		//调用service方法，进行用户匹配
		User user = userService.login(userCode,userPassword);
		if(null != user){//登录成功
			//放入session
			session.setAttribute(Constants.USER_SESSION, user);
			//页面跳转（frame.jsp）
			//response.sendRedirect("jsp/frame.jsp");
			return "redirect:/main.html";
		}else{
			//页面跳转（login.jsp）带出提示信息--转发
			//request.setAttribute("error", "用户名或密码不正确");
			throw new RuntimeException("用户名或密码不正确");
			//model.addAttribute("error", "用户名或密码不正确");
			//request.getRequestDispatcher("login.jsp").forward(request, response);
			//return "error";
		}
	}*/
	
	@RequestMapping("/sys/main.html")
	public String main(HttpSession session) {
		User userSession = (User)session.getAttribute(Constants.USER_SESSION);
		if(null == userSession){
			return "login";
		}
		return "frame";
	}
	
	@RequestMapping("/logout")
	public String logout(HttpSession session) {
		session.removeAttribute(Constants.USER_SESSION);
		return "login";
	}
	
    /*
     * 局部异常处理
     */
/*	@ExceptionHandler(value={RuntimeException.class})
	public String exceptionHandler(RuntimeException e,HttpServletRequest request) {
		request.setAttribute("e", e);
		return "login";
	}*/
}
